<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:bil="http://java.sun.com/jsf/composite/bill"
      xmlns:phi="http://java.sun.com/jsf/composite/pharmacy"
      xmlns:pa="http://java.sun.com/jsf/composite/paymentMethod"
      xmlns:pat="http://java.sun.com/jsf/composite/patient">

    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">


                <h:form id="bill" >

                    <p:commandButton id="nullButton2" value="No Action" action="#" style="display: none;" ></p:commandButton>
                    <p:defaultCommand  target="btnAdd" />  



                    <p:panel rendered="#{!pharmacySaleController.billPreview}"  >


                        <h:panelGrid id="sale" columns="2" class="alignTop" >

                            <p:panel style="background-color: grey;" >
                                <f:facet name="header" >
                                    <h:outputLabel value="Pharmacy Retail Bill" />
                                </f:facet>

                                <h:panelGrid columns="1" >

                                    <p:panel id="panelErrorMsg"  >
                                        <h:outputLabel value="#{pharmacySaleController.errorMessage}" style="padding: 5px; margin: 5px; color: red; background-color: yellow ; border: 1px solid red;"
                                                       rendered="#{pharmacySaleController.errorMessage ne ''}">
                                        </h:outputLabel>
                                    </p:panel>

                                    <h:panelGrid columns="6" >

                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Medicines/Devices" ></p:outputLabel>

                                            <style>
                                                .ui-autocomplete-input {width:350px!important;display: block;}
                                                .ui-autocomplete{display: block!important;}
                                            </style>
                                            <p:autoComplete accesskey="i"    id="acStock" 
                                                            value="#{pharmacySaleController.stock}" 
                                                            converter="stockCon" 
                                                            completeMethod="#{pharmacySaleController.completeAvailableStocksFromNameOrGeneric}" 
                                                            var="i" itemLabel="#{i.itemBatch.item.name}" itemValue="#{i}" 
                                                            >
                                                <p:column headerText="Item">
                                                    <h:outputLabel value="#{i.itemBatch.item.name}"  style="width: 300px!important;"></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Code">
                                                    <h:outputLabel value="#{i.itemBatch.item.code}" style="width: 50px!important;"></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Generic">
                                                    <h:outputLabel value="#{i.itemBatch.item.vmp.name}" style="width: 150px!important;"></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Rate">
                                                    <h:outputLabel value="#{i.itemBatch.retailsaleRate}"  style="width: 50px!important;">
                                                        <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Stocks">
                                                    <h:outputLabel value="#{i.stock}"  style="width: 50px!important;">
                                                        <f:convertNumber pattern="#,###" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Expiry">
                                                    <h:outputLabel value="#{i.itemBatch.dateOfExpire}"  
                                                                   style="width: 100px!important;#{commonController.currentDateTime > i.itemBatch.dateOfExpire ?'color: red; background-color: yellow;':''}">
                                                        <f:convertDateTime pattern="dd/MM/yyyy" ></f:convertDateTime>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:ajax event="focus" process="acStock :#{p:component('cmbPs')}"  ></p:ajax>
                                                <p:ajax event="itemSelect"   listener="#{pharmacySaleController.handleSelect}"  update="txtQty txtRate focusQty :#{p:component('lstReplaceableBatch')}" ></p:ajax>
                                                <p:ajax event="query" update=":#{p:component('tabI')}" process="acStock"></p:ajax>
                                            </p:autoComplete>

                                        </h:panelGrid>
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Quantity" ></p:outputLabel>
                                            <p:inputText  accesskey="q"  autocomplete="off" id="txtQty" value="#{pharmacySaleController.intQty}" style="width: 50px!important;"   >
                                                <p:ajax event="keyup"  listener="#{pharmacySaleController.calculateBillItemListner}" process="acStock txtQty :#{p:component('cmbPs')}" update="txtRate txtVal" ></p:ajax>
                                                <p:ajax event="blur"  listener="#{pharmacySaleController.calculateBillItemListner}" process="acStock txtQty :#{p:component('cmbPs')}" update="txtRate txtVal" ></p:ajax>
                                            </p:inputText>
                                        </h:panelGrid>

                                        <p:commandButton  accesskey="a" id="btnAdd" value="Add" 
                                                         action="#{pharmacySaleController.addBillItemNew()}" 
                                                          process="@this acStock txtQty :#{p:component('cmbPs')}" 
                                                          update=":#{p:component('netTotal')} :#{p:component('dis')} :#{p:component('total')} :#{p:component('pBillDetails')} :#{p:component('tblBillItem')} txtRate txtQty acStock focusItem :#{p:component('tabI')}" ></p:commandButton>

                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Rate" ></p:outputLabel>
                                            <p:outputLabel id="txtRate" value="#{pharmacyWholeSaleController.billItem.netRate}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </p:outputLabel>
                                        </h:panelGrid>
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Value" ></p:outputLabel>
                                            <p:outputLabel id="txtVal" value="#{pharmacySaleController.billItem.netValue}" >
                                                <f:convertNumber pattern="#,##0.00" />
                                            </p:outputLabel>
                                        </h:panelGrid>


                                        <p:focus id="focusQty" for="txtQty" ></p:focus>
                                        <p:focus id="focusItem" for="acStock" ></p:focus>

                                    </h:panelGrid>


                                    <h:panelGrid columns="1">
                                        <p:panel header="Bill Items" id="pBis">

                                            <p:dataTable id="tblBillItem" value="#{pharmacySaleController.preBill.billItems}"
                                                         var="bi" rowIndexVar="s" editable="true" sortBy="#{bi.searialNo}" >

                                                <p:ajax event="rowEdit" listener="#{pharmacySaleController.onEdit}" update="@this gros :#{p:component('pBillDetails')}" />  
                                                <p:ajax event="rowEditCancel" listener="#{pharmacySaleController.onEdit}" update="@this gros :#{p:component('pBillDetails')}" /> 


                                                <p:column headerText="Item">
                                                    <h:outputLabel value="#{bi.pharmaceuticalBillItem.itemBatch.item.name}" ></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Quantity">
                                                    <p:cellEditor>  
                                                        <f:facet name="output">  
                                                            <h:outputText value="#{bi.qty}" >
                                                                <f:convertNumber integerOnly="true" />
                                                            </h:outputText>
                                                        </f:facet>  
                                                        <f:facet name="input">  
                                                            <p:inputText autocomplete="off" id="ipTblQty" value="#{bi.qty}" style="width:96%"/>  
                                                        </f:facet>  
                                                    </p:cellEditor> 
                                                </p:column>

                                                <p:column style="width:6%">  
                                                    <p:rowEditor />  
                                                </p:column>  


                                                <p:column headerText="Rate">
                                                    <h:outputLabel id="rate" value="#{bi.rate}" >
                                                        <f:convertNumber pattern="#,##0.00" />
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Value">
                                                    <h:outputLabel value="#{bi.grossValue}" id="gros">
                                                        <f:convertNumber pattern="#,##0.00" />
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Expiry">
                                                    <h:outputLabel value="#{bi.pharmaceuticalBillItem.itemBatch.dateOfExpire}" >
                                                        <f:convertDateTime pattern="dd MM yyyy" ></f:convertDateTime>
                                                    </h:outputLabel>

                                                </p:column>
                                                <p:column headerText="Remove">
                                                    <p:commandButton value="Remove" action="#{pharmacySaleController.removeBillItem(bi)}" ajax="false" >

                                                    </p:commandButton>
                                                </p:column>

                                            </p:dataTable>

                                        </p:panel>
                                    </h:panelGrid>


                                </h:panelGrid>
                                <p:panel header="Availability" id="tabI" >

                                    <p:panelGrid columns="2" style="width: 100%;vertical-align: top;" styleClass="alignTop">

                                        <h:panelGroup >
                                            <h:outputLabel value="These " ></h:outputLabel>
                                            <b>
                                                <h:outputLabel value=" available Stocks " ></h:outputLabel>
                                            </b>

                                            <h:outputLabel value=" that can be used as " ></h:outputLabel>
                                            <b>
                                                <h:outputLabel value=" replacement." ></h:outputLabel>
                                            </b>




                                        </h:panelGroup>


                                        <h:panelGroup >
                                            <h:outputLabel value="Stocks " escape="false" ></h:outputLabel>
                                            <b>
                                                <h:outputLabel value=" NOT " escape="false" ></h:outputLabel>

                                                <i>
                                                    <h:outputLabel value=" available " escape="false" ></h:outputLabel>
                                                </i>
                                            </b>
                                            <h:outputLabel value=" for these tiems." escape="false" ></h:outputLabel>

                                        </h:panelGroup>

                                        <p:dataTable id="lstReplaceableBatch" 
                                                     selection="#{pharmacySaleController.replacableStock}"
                                                     value="#{pharmacySaleController.replaceableStocks}" 
                                                     var="ri" 
                                                     selectionMode="single"
                                                     rowKey="#{ri.id}"
                                                     emptyMessage="System can not detect suitable replacements.">
                                            <p:ajax event="rowSelect" process="@this" update=":#{p:component('acStock')}" listener="#{pharmacySaleController.makeStockAsBillItemStock()}" ></p:ajax>
                                            <p:ajax event="rowUnselect" process="@this" update=":#{p:component('acStock')}" listener="#{pharmacySaleController.makeStockAsBillItemStock()}" ></p:ajax>
                                            <p:column headerText="Item">
                                                #{ri.itemBatch.item.name}
                                            </p:column>
                                            <p:column headerText="Code">
                                                #{ri.itemBatch.item.code}
                                            </p:column>
                                            <p:column headerText="Rate"  style="text-align: right;">
                                                <p:outputLabel value="#{ri.itemBatch.retailsaleRate}">
                                                    <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                                                </p:outputLabel>
                                            </p:column>
                                            <p:column headerText="Stocks" style="text-align: right;">
                                                <p:outputLabel value="#{ri.stock}"  style="width: 50px!important;">
                                                    <f:convertNumber pattern="#,###" ></f:convertNumber>
                                                </p:outputLabel>
                                            </p:column>
                                            <p:column headerText="Expiry">
                                                <h:panelGroup rendered="#{commonController.currentDateTime > ri.itemBatch.dateOfExpire}" >
                                                    <p:outputLabel value="#{ri.itemBatch.dateOfExpire}" styleClass="ui-messages-fatal"  >
                                                        <f:convertDateTime pattern="dd/MM/yy" ></f:convertDateTime>
                                                    </p:outputLabel>
                                                </h:panelGroup>
                                                <h:panelGroup rendered="#{ri.itemBatch.dateOfExpire > commonController.currentDateTime}" >
                                                    <p:outputLabel value="#{ri.itemBatch.dateOfExpire}" >
                                                        <f:convertDateTime pattern="dd/MM/yy" ></f:convertDateTime>
                                                    </p:outputLabel>
                                                </h:panelGroup>
                                            </p:column>
                                        </p:dataTable>

                                        <p:dataTable id="lstSelectItems" 
                                                     selection="#{pharmacySaleController.selectedAvailableAmp}"
                                                     value="#{pharmacySaleController.itemsWithoutStocks}" 
                                                     var="si" 
                                                     selectionMode="single"
                                                     rowKey="#{si.id}"
                                                     emptyMessage="No items with that name without stocks.">
                                            <p:ajax event="rowSelect" process="@this" update=":#{p:component('lstReplaceableBatch')}" listener="#{pharmacySaleController.selectReplaceableStocksNew()}" ></p:ajax>
                                            <p:ajax event="rowUnselect" process="@this" update=":#{p:component('lstReplaceableBatch')}" listener="#{pharmacySaleController.selectReplaceableStocksNew()}" ></p:ajax>
                                            <p:column headerText="Generic">
                                                #{si.vmp.name}
                                            </p:column>
                                            <p:column headerText="Name">
                                                <span class="ui-messages-info-icon" ></span>
                                                #{si.name}
                                            </p:column>

                                        </p:dataTable>
                                    </p:panelGrid>
                                </p:panel> 

                            </p:panel>

                            <p:panel>


                                <f:facet name="header" >
                                    <h:panelGrid columns="20"  >

                                        <h:outputLabel value="&nbsp;&nbsp;&nbsp;&nbsp;" style="min-width: 50px;" />
                                        <p:selectOneMenu   id="pay" value="#{pharmacySaleController.paymentMethod}">    
                                            <f:selectItems value="#{enumController.paymentMethodsWithoutCredit}"/>
                                            <p:ajax process="@this cmbPs " 
                                                    update="pBillDetails creditCard cheque slip" 
                                                    event="change"  
                                                    listener="#{pharmacySaleController.changeListener()}"/>
                                        </p:selectOneMenu>

                                        <p:selectOneMenu   id="cmbPs" value="#{pharmacySaleController.paymentScheme}">   
                                            <f:selectItem itemLabel="Discount Scheme"/>
                                            <f:selectItems value="#{paymentSchemeController.paymentSchemesForPharmacy}" var="i" itemLabel="#{i.name}" itemValue="#{i}"/>
                                            <p:ajax process="@this pay " 
                                                    update="pBillDetails creditCard cheque slip" 
                                                    event="change"  
                                                    listener="#{pharmacySaleController.changeListener()}"/>
                                        </p:selectOneMenu>



                                        <h:panelGroup id="creditCard" style="display: #{pharmacySaleController.paymentMethod ne 'Card' ? 'none' : 'block'} ; ">
                                            <pa:creditCard paymentMethodData="#{pharmacySaleController.paymentMethodData}"/>
                                        </h:panelGroup>

                                        <h:panelGroup id="cheque" style="display: #{pharmacySaleController.paymentMethod ne 'Cheque' ? 'none' : 'block'} ; " >
                                            <pa:cheque paymentMethodData="#{pharmacySaleController.paymentMethodData}"/>
                                        </h:panelGroup>

                                        <h:panelGroup id="slip" style="display: #{pharmacySaleController.paymentMethod ne 'Slip' ? 'none' : 'block'} ;">
                                            <pa:slip paymentMethodData="#{pharmacySaleController.paymentMethodData}"/>
                                        </h:panelGroup>
                                        <p:commandButton accesskey="c" ajax="false" value="For Cashier"
                                                        action="#{pharmacySaleController.settlePreBill()}" 
                                                         onclick="onSubmitButton();" ></p:commandButton>
                                        <p:commandButton accesskey="n" value="New Pharmacy Bill" ajax="false" 
                                                        actionListener="#{pharmacySaleController.resetAll}"  ></p:commandButton>


                                    </h:panelGrid>




                                </f:facet>

                                <p:panelGrid columns="2">
                                    <p:outputLabel value="Referring Doctor" ></p:outputLabel>
                                    <p:autoComplete forceSelection="true" id="cmbDoc" value="#{pharmacySaleController.preBill.referredBy}" 
                                                    completeMethod="#{doctorController.completeDoctor}" 
                                                    var="ix" itemLabel="#{ix.person.name}" 
                                                    itemValue="#{ix}" size="30"  
                                                    >
                                    </p:autoComplete>
                                    <p:outputLabel value="Comments" ></p:outputLabel>
                                    <p:inputTextarea value="#{pharmacySaleController.comment}" id="comment"/>
                                </p:panelGrid>


                                <p:panel header="Bill Details" id="pBillDetails"  >
                                    <p:panelGrid columns="2" columnClasses="numberCol, textCol"  >
                                        <h:outputLabel value="Total" ></h:outputLabel>
                                        <h:outputLabel id="total" value="#{pharmacySaleController.preBill.total}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>
                                        <h:outputLabel value="Discount" ></h:outputLabel>
                                        <h:outputLabel id="dis" value="#{pharmacySaleController.preBill.discount}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                            <p:ajax process="total dis netTotal paid" update="balance netTotal" event="blur"/>
                                        </h:outputLabel>
                                        <h:outputLabel value="Net Total" ></h:outputLabel>
                                        <h:outputLabel id="netTotal" value="#{pharmacySaleController.preBill.netTotal}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>
                                        <h:outputLabel  value="Tendered" ></h:outputLabel>
                                        <p:inputText accesskey="t" autocomplete="off" id="paid" value="#{pharmacySaleController.cashPaid}" >
                                            <p:ajax process="total dis netTotal paid" update="balance netTotal" event="blur"/>
                                        </p:inputText>
                                        <h:outputLabel  value="Balance " ></h:outputLabel>
                                        <h:outputLabel id="balance" value="#{pharmacySaleController.balance}" >
                                            <f:convertNumber pattern="#,##0.00" />
                                        </h:outputLabel>
                                    </p:panelGrid>

                                </p:panel>

                                <p:panel header="Patient Detail">                                        
                                    <p:tabView id="tvPt" style="min-height: 250px;" activeIndex="#{pharmacySaleController.patientSearchTab}"  >
                                        <p:ajax event="tabChange"  process="@this"
                                                update=":#{p:component('cmbPs')} :#{p:component('pBillDetails')}"
                                                listener="#{pharmacySaleController.onTabChange}" />

                                        <p:tab id="tabNewPt" title="New Patient"  >
                                            <bil:newPatient controller="#{pharmacySaleController}"/>
                                        </p:tab>

                                        <p:tab id="tabSearchPt" title="Search Patient" >


                                            <p:autoComplete
                                                widgetVar="aPt" 
                                                id="acPt" forceSelection="true" 
                                                value="#{pharmacySaleController.searchedPatient}" 
                                                completeMethod="#{patientController.completePatientByNameOrCode}" 
                                                var="apt" itemLabel="#{apt.person.name}" 
                                                itemValue="#{apt}" size="30"  
                                                style="width: 400px;"
                                                disabled="#{pharmacySaleController.fromOpdEncounter}">
                                                <p:ajax event="itemSelect" process="@this" update=":#{p:component('cmbPs')} :#{p:component('pBillDetails')} panSearch"
                                                        listener="#{pharmacySaleController.searchPatientListener}"/>
                                            </p:autoComplete>  
                                            <h:panelGroup id="panSearch">
                                                <pat:searchPatientDetail patient="#{pharmacySaleController.searchedPatient}"  />
                                            </h:panelGroup>
                                            <div style="padding: 2px; margin: 2px; border: 1px solid;">
                                                <p:outputLabel value="#{pharmacySaleController.opdEncounterComments}" ></p:outputLabel>
                                            </div>
                                        </p:tab>                                    </p:tabView>
                                </p:panel>



                            </p:panel>

                        </h:panelGrid>

                    </p:panel>


                </h:form>

                <h:form>


                    <p:panel  rendered="#{pharmacySaleController.billPreview}" >

                        <p:commandButton id="nullButton3" value="No Action" action="#" style="display: none;" ></p:commandButton>
                        <p:defaultCommand  target="btnPrint" />  

                        <div class="nonPrintBlock" >
                            <p:commandButton accesskey="p" id="btnPrint" value="Print" ajax="false" action="#" >
                                <p:printer target="gpBillPreview" ></p:printer>
                            </p:commandButton>
                            <p:commandButton accesskey="n" value="New Pharmacy Bill" ajax="false" action="#{pharmacySaleController.resetAll()}" ></p:commandButton>

                        </div>

                        <h:panelGroup   id="gpBillPreview"  > 

                            <h:panelGroup   id="gpBillPreviewSingle" rendered="#{sessionController.loggedPreference.pharmacyBillPaperType eq 'PosPaper'}">

                                <div >
                                    <h:panelGroup rendered="#{sessionController.loggedPreference.pharmacyBillPrabodha eq false}" > 
                                        <phi:saleBill bill="#{pharmacySaleController.printBill}"></phi:saleBill>
                                    </h:panelGroup>
                                </div>

                            </h:panelGroup>

                            <h:panelGroup id="gpBillPreviewDouble" rendered="#{sessionController.loggedPreference.pharmacyBillPaperType eq 'PosPaper'}"> 

                                <div >
                                    <h:panelGroup     rendered="#{sessionController.loggedPreference.pharmacyBillPrabodha}">                             
                                        <phi:saleBill_prabodha bill="#{pharmacySaleController.printBill}"></phi:saleBill_prabodha>
                                    </h:panelGroup>
                                </div>

                            </h:panelGroup>

                            <h:panelGroup id="gpBillPreviewFiveFive" rendered="#{sessionController.loggedPreference.pharmacyBillPaperType eq 'FiveFivePaper'}"> 

                                <div >
                                    <h:panelGroup rendered="#{sessionController.loggedPreference.pharmacyBillPrabodha eq false}" > 
                                        <phi:saleBill_five_five bill="#{pharmacySaleController.printBill}"></phi:saleBill_five_five>
                                    </h:panelGroup>
                                </div>

                            </h:panelGroup>

                        </h:panelGroup>



                    </p:panel>



                </h:form>

            </ui:define>

        </ui:composition>

    </h:body>
</html>
